const mongoose = require('mongoose');
const { generateAvailableTimes, getNextAvailableTime } = require('./generate-times');

// 数据库连接
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://3173302147:daDqJorJy2gEwynZ@cluster0.wvtdip1.mongodb.net/yiliao';

// 科室模型定义
const departmentSchema = new mongoose.Schema({
  name: { type: String, required: true, trim: true },
  description: { type: String, default: '' },
  hospitalCount: { type: Number, default: 0 },
  priceRange: { type: String, default: '50-100' },
  availableTimes: {
    type: [{
      date: { type: String, required: true },
      timeSlots: [{
        time: { type: String, required: true },
        isAvailable: { type: Boolean, default: true },
        patientId: { type: String, default: null }
      }]
    }],
    default: []
  },
  icon: { type: String, default: 'https://img.icons8.com/color/48/000000/doctor-male.png' },
  status: { type: String, enum: ['active', 'inactive'], default: 'active' },
  sortOrder: { type: Number, default: 0 },
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
});

const Department = mongoose.model('Department', departmentSchema);

// 生成科室基础数据
function generateDepartmentsData() {
  const baseDepartments = [
    {
      name: '内科',
      description: '内科是临床医学的一个分支，主要治疗内脏器官疾病',
      hospitalCount: 15,
      priceRange: '50-200',
      icon: 'https://img.icons8.com/color/48/000000/doctor-male.png',
      status: 'active',
      sortOrder: 1
    },
    {
      name: '外科',
      description: '外科是临床医学的一个分支，主要进行手术治疗',
      hospitalCount: 12,
      priceRange: '100-500',
      icon: 'https://img.icons8.com/color/48/000000/surgeon.png',
      status: 'active',
      sortOrder: 2
    },
    {
      name: '儿科',
      description: '儿科专门治疗儿童疾病，包括新生儿到青少年',
      hospitalCount: 8,
      priceRange: '60-150',
      icon: 'https://img.icons8.com/color/48/000000/baby-bottle.png',
      status: 'active',
      sortOrder: 3
    },
    {
      name: '妇产科',
      description: '妇产科专门治疗女性生殖系统疾病和妊娠相关疾病',
      hospitalCount: 10,
      priceRange: '80-300',
      icon: 'https://img.icons8.com/color/48/000000/pregnant.png',
      status: 'active',
      sortOrder: 4
    },
    {
      name: '眼科',
      description: '眼科专门治疗眼部疾病和视力问题',
      hospitalCount: 6,
      priceRange: '100-400',
      icon: 'https://img.icons8.com/color/48/000000/eye.png',
      status: 'active',
      sortOrder: 5
    },
    {
      name: '口腔科',
      description: '口腔科专门治疗口腔、牙齿和颌面部疾病',
      hospitalCount: 9,
      priceRange: '50-300',
      icon: 'https://img.icons8.com/color/48/000000/dental-care.png',
      status: 'active',
      sortOrder: 6
    },
    {
      name: '皮肤科',
      description: '皮肤科专门治疗皮肤、毛发和指甲疾病',
      hospitalCount: 7,
      priceRange: '60-200',
      icon: 'https://img.icons8.com/color/48/000000/skin.png',
      status: 'active',
      sortOrder: 7
    },
    {
      name: '骨科',
      description: '骨科专门治疗骨骼、关节和肌肉疾病',
      hospitalCount: 11,
      priceRange: '120-600',
      icon: 'https://img.icons8.com/color/48/000000/bone.png',
      status: 'active',
      sortOrder: 8
    }
  ];

  // 为每个科室生成可用时间段
  return baseDepartments.map(dept => ({
    ...dept,
    availableTimes: generateAvailableTimes()
  }));
}

async function addDataToDatabase() {
  try {
    console.log('正在连接到MongoDB数据库...');
    await mongoose.connect(MONGODB_URI);
    console.log('✅ 数据库连接成功！');

    // 清空现有数据（可选）
    console.log('正在清空现有科室数据...');
    await Department.deleteMany({});
    console.log('✅ 现有数据已清空');

    // 生成科室数据
    console.log('正在生成科室数据...');
    const departmentsData = generateDepartmentsData();

    // 添加新数据
    console.log('正在添加科室数据到数据库...');
    const result = await Department.insertMany(departmentsData);
    console.log(`✅ 成功添加 ${result.length} 个科室到数据库`);

    // 显示添加的数据和下一个可用时间
    console.log('\n📋 已添加的科室列表:');
    result.forEach((dept, index) => {
      const nextTime = getNextAvailableTime(dept.availableTimes);
      console.log(`${index + 1}. ${dept.name} - ${dept.description}`);
      console.log(`   下一个可用时间: ${nextTime}`);
    });

    // 验证数据是否添加成功
    const totalCount = await Department.countDocuments();
    console.log(`\n📊 数据库中总共有 ${totalCount} 个科室`);

    console.log('\n🎉 数据添加完成！');
    process.exit(0);

  } catch (error) {
    console.error('❌ 添加数据失败:', error);
    process.exit(1);
  }
}

// 运行脚本
addDataToDatabase(); 